import React, {
    Component,
} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Platform,
    Dimensions,
    TouchableOpacity,
    Image,
    Alert,
    FlatList,
    TextInput,
    Linking,
    DeviceEventEmitter,
    ScrollView,
    ActivityIndicator
} from 'react-native';

import moment from 'moment';
import HttpUtils from '../../utils/HttpUtils';
import HttpUrls from '../../utils/HttpUrls';
import AndroidModule from "../../utils/AndroidConnent";
import { ToastToUser } from "../../utils/CustomToast";
import Constant from '../../utils/Constant'
import NoData from '../../utils/NoData'
import Loading from "../../utils/Loading";
import { getImgUrl } from '../../utils/ComFunction'
import { conditionObjWithType } from '../../utils/Conditions'
import AsyncImage from '../../components/image/AsyncImage';
import { OrderStatus, LogisticsStatus, detectionStatus, payType } from '../../utils/Enum';

let pageSize = 20
let pageNo = 1;
let itemNo = 0;

let navigation

// 0：待签署，10：待发货，20：待收货，30：待预付，40：待结算，50：售后中

export default class OrderListItemView extends Component {
    constructor(props) {
        super(props);
        let orderStatus = this.props.orderStatus != undefined ? this.props.orderStatus : '';
        this.state = {
            keyword: this.props.keyword,
            isFranchisee: Constant.userInfo.isFranchisee,
            orderStatus,
            orderList: [],
            isRefresh: false,
            showFoot: 0, // 控制foot， 0：隐藏footer  1：已加载完成,没有更多数据   2 ：显示加载中
        }
    }

    componentWillUnmount() {
        if (this.refreshOrderListListener) {
            this.refreshOrderListListener.remove();
        }
    }

    componentWillReceiveProps(nextProps) {
        if (this.state.keyword != nextProps.keyword && nextProps.currentIndex == nextProps.index) {
            this.setState({
                keyword: nextProps.keyword,
            }, function () {
                this.getOrders(true);
            });
        }
    }

    componentDidMount() {
        this.getOrders(true);
    }

    render() {
        navigation = this.props.navigation;
        return (
            <View style={styles.container}>
                <FlatList
                    style={{ flex: 1, height: '100%' }}
                    keyExtractor={this._keyExtractor}
                    data={this.state.orderList}
                    renderItem={this.renderOrderItem.bind(this)}
                    onEndReachedThreshold={0.2}
                    onEndReached={() => this.reachedEnd()}
                    ListFooterComponent={() => this.renderFooter()}
                    ListEmptyComponent={this.renderEmpty.bind(this)}
                    onRefresh={this.refreshData}
                    refreshing={this.state.isRefresh}
                />

                <Loading onRef={(ref) => (this.Loading = ref)} />

            </View>
        )
    }

    renderOrderItem({ item, index }) {
        return (
            <View style={styles.orderBg}>
                <View style={styles.sepLine} />
                <TouchableOpacity
                    style={styles.orderContainer}
                    activeOpacity={0.8}
                    onPress={() => {
                        console.log('orderInfo=', item)
                        navigation.navigate('NormalOrderDetail', { orderNo: item.value.orderNo })
                    }}
                >
                    <View style={styles.topContainer}>
                        <Text style={styles.companyText}>{this.state.isFranchisee ? item.value.buyerMerchantAbbreviation : item.value.sellerMerchantAbbreviation}</Text>
                        <Text style={styles.statusText}>{conditionObjWithType('orderStatus')[item.value.orderStatus]}</Text>
                    </View>
                    <View style={styles.carContainer}>
                        <AsyncImage
                            source={{ uri: getImgUrl(item.value.mainPic) }}
                            defaultSource={require('source/image/icon_car_default.png')}
                            style={styles.carImg}
                            resizeMode="cover"
                        />
                        <View style={styles.carInfoBox}>
                            <Text style={styles.brandText} numberOfLines={2}>{this.fromatBrand(item)}</Text>
                            <Text style={styles.carInfoText}>{this.formatInfo(item)}</Text>
                            <Text style={styles.priceText}>{'成交价' + (item.value.tradePrice / (10000 * 100)).toFixed(2) + '万元'}</Text>
                        </View>
                    </View>
                    {
                        this.state.isFranchisee ?
                            this.renderFranchiseeBtn(item.value)
                            :
                            this.renderMerchantBtn(item.value)
                    }
                </TouchableOpacity>
            </View>
        )
    }

    renderFranchiseeBtn(item) {//销售订单
        return (
            <View style={styles.btnBg}>
                {
                    item.orderStatus == OrderStatus.OrderStatusWaitSign || item.orderStatus == OrderStatus.OrderStatusRejectSign ?
                        this.renderFranchiseeWaitSignBottom(item)
                        :
                        item.orderStatus == OrderStatus.OrderStatusSigning ?
                            this.rendeFranchiseeSigningBottom(item)
                            :
                            item.orderStatus == OrderStatus.OrderStatusWaitDelivery ?
                                this.renderWaitSendCarBottom(item)
                                :
                                item.orderStatus == OrderStatus.OrderStatusWaitPay ?
                                    this.renderFranchiseeWaitPayBottom(item)
                                    :
                                    item.orderStatus == OrderStatus.OrderStatusWaitReceipt ?
                                        this.renderFranchiseeWaitReceiveBottom(item)
                                        :
                                        item.orderStatus == OrderStatus.OrderStatusWaitSettlement ?
                                            this.renderFranchiseeWaitSettlementBottom(item)
                                            :
                                            item.orderStatus == OrderStatus.OrderStatusServicing ?
                                                this.renderAfterSaleBottom(item)
                                                :
                                                null
                }
            </View>
        )
    }

    renderMerchantBtn(item) {//采购订单
        return (
            <View style={styles.btnBg}>
                {
                    item.orderStatus == OrderStatus.OrderStatusWaitSign || item.orderStatus == OrderStatus.OrderStatusRejectSign ?
                        this.renderMerchantWaitSignBottom(item)
                        :
                        item.orderStatus == OrderStatus.OrderStatusSigning ?
                            this.renderMerchantSigningBottom(item)
                            :
                            item.orderStatus == OrderStatus.OrderStatusWaitDelivery ?
                                this.renderWaitSendCarBottom(item)
                                :
                                item.orderStatus == OrderStatus.OrderStatusWaitPay ?
                                    this.renderMerchantWaitPayBottom(item)
                                    :
                                    item.orderStatus == OrderStatus.OrderStatusWaitReceipt ?
                                        this.renderMerchantWaitReceiveBottom(item)
                                        :
                                        item.orderStatus == OrderStatus.OrderStatusWaitSettlement ?
                                            this.renderMerchantWaitSettlementBottom(item)
                                            :
                                            item.orderStatus == OrderStatus.OrderStatusServicing ?
                                                this.renderAfterSaleBottom(item)
                                                :
                                                null
                }
            </View>
        )
    }

    //卖家待签署状态：0
    renderFranchiseeWaitSignBottom(item) {
        // tsk 如果是全款支付订单，跳过检测流程，以适应原来流程的UI
        if(item.payType == payType.payTypeFull){
            item.detectionStatus = detectionStatus.detectionStatusDetected
        }
        return (
            <View style={styles.btnBg}>
                <TouchableOpacity
                    style={styles.btnBox}
                    onPress={() => {
                        this.consultOnline(item)
                    }}
                >
                    <Text style={styles.btnText}>{'联系买家'}</Text>
                </TouchableOpacity>
                {
                    item.detectionStatus == detectionStatus.detectionStatusDetected ?
                        <TouchableOpacity
                            style={styles.btnBox1}
                            onPress={() => {
                                navigation.navigate('NormalOrderDetail', { orderNo: item.orderNo })
                            }}
                        >
                            <Text style={styles.btnText}>{'查看订单'}</Text>
                        </TouchableOpacity>
                        :
                        <TouchableOpacity
                            style={styles.btnBox1}
                            onPress={() => {
                                console.log('detectionStatus', item.detectionStatus)
                                if (item.detectionStatus == detectionStatus.detectionStatusDetecting) {
                                    ToastToUser('检测订单已提交，请去检测中心处理')
                                    return
                                }
                                console.log('CarDetectionPage')
                                navigation.navigate('CarDetectionPage', {
                                    carId: item.carId, completion: () => {
                                        let orderList = this.state.orderList
                                        orderList.map((orderItem) => {
                                            if (orderItem.value.orderNo == item.orderNo) {
                                                orderItem.value.detectionStatus = detectionStatus.detectionStatusWaitForDetection
                                            }
                                        })
                                        this.setState({
                                            orderList: orderList
                                        })
                                    }
                                })
                            }}
                        >
                            <Text style={styles.btnText}>{'立即检测'}</Text>
                        </TouchableOpacity>
                }
            </View>
        )
    }

    //买家待签署状态：0
    renderMerchantWaitSignBottom(item) {
        // tsk 如果是全款支付订单，跳过检测流程，以适应原来流程的UI
        if(item.payType == payType.payTypeFull){
            item.detectionStatus = detectionStatus.detectionStatusDetected
        }
        return (
            <View style={styles.btnBg}>
                <TouchableOpacity
                    style={styles.btnBox}
                    onPress={() => {
                        this.consultOnline(item)
                    }}
                >
                    <Text style={styles.btnText}>{'联系卖家'}</Text>
                </TouchableOpacity>
                {
                    item.detectionStatus == detectionStatus.detectionStatusDetected ?
                        <TouchableOpacity
                            style={styles.btnBox1}
                            onPress={() => {
                                navigation.navigate('SignMainBodyPage', { orderNo: item.orderNo })
                            }}
                        >
                            <Text style={styles.btnText}>{'签署合同'}</Text>
                        </TouchableOpacity>
                        :
                        item.applyDetection != 1 ?
                            <TouchableOpacity
                                style={styles.btnBox1}
                                onPress={() => {
                                    this.applyDetection(item)
                                }}
                            >
                                <Text style={styles.btnText}>{'催促检测'}</Text>
                            </TouchableOpacity>
                            :
                            null
                }
            </View>
        )
    }

    //卖家签署中状态
    rendeFranchiseeSigningBottom(item) {
        return (
            <View style={styles.btnBg}>
                <View style={styles.btnBg}>
                    <TouchableOpacity
                        style={styles.btnBox}
                        onPress={() => {
                            this.consultOnline(item)
                        }}
                    >
                        <Text style={styles.btnText}>{'联系买家'}</Text>
                    </TouchableOpacity>
                    {
                        item.sellerContractSign != 1 ?
                            <TouchableOpacity
                                style={styles.btnBox1}
                                onPress={() => {
                                    navigation.navigate('ContractDetailPage', { contractNo: item.contractNo, orderNo: item.orderNo })
                                }}
                            >
                                <Text style={styles.btnText}>{'签署合同'}</Text>
                            </TouchableOpacity>
                            :
                            <TouchableOpacity
                                style={styles.btnBox1}
                                onPress={() => {
                                    navigation.navigate('ContractDetailPage', { contractNo: item.contractNo, orderNo: item.orderNo })
                                }}
                            >
                                <Text style={styles.btnText}>{'查看合同'}</Text>
                            </TouchableOpacity>
                    }

                </View>
            </View>
        )
    }

    //买家签署中状态
    renderMerchantSigningBottom(item) {
        return (
            <View style={styles.btnBg}>
                <View style={styles.btnBg}>
                    <TouchableOpacity
                        style={styles.btnBox}
                        onPress={() => {
                            this.consultOnline(item)
                        }}
                    >
                        <Text style={styles.btnText}>{'联系卖家'}</Text>
                    </TouchableOpacity>
                    {
                        item.buyerContractSign != 1 ?
                            <TouchableOpacity
                                style={styles.btnBox1}
                                onPress={() => {
                                    navigation.navigate('ContractDetailPage', { contractNo: item.contractNo, orderNo: item.orderNo })
                                }}
                            >
                                <Text style={styles.btnText}>{'签署合同'}</Text>
                            </TouchableOpacity>
                            :
                            <TouchableOpacity
                                style={styles.btnBox1}
                                onPress={() => {
                                    navigation.navigate('ContractDetailPage', { contractNo: item.contractNo, orderNo: item.orderNo })
                                }}
                            >
                                <Text style={styles.btnText}>{'查看合同'}</Text>
                            </TouchableOpacity>
                    }

                </View>
            </View>
        )
    }

    //买家/卖家待发货状态：1
    renderWaitSendCarBottom(item) {
        return (
            <View style={styles.btnBg}>
                {
                    // 双方未确认
                    item.shippingConfirm != 1 && item.deliveryConfirm != 1 ?
                        <View style={styles.btnBg}>
                            <TouchableOpacity
                                style={styles.btnBox}
                                onPress={() => {
                                    this.consultOnline(item)
                                }}
                            >
                                <Text style={styles.btnText}>{this.state.isFranchisee ? '联系买家' : '联系卖家'}</Text>
                            </TouchableOpacity>
                            <TouchableOpacity
                                style={styles.btnBox1}
                                onPress={() => {
                                    navigation.navigate('LogisticsOrderingSubmitPage', {
                                        orderNo: item.orderNo, submitHandler: (logisticsNo) => {
                                            let orderList = this.state.orderList
                                            orderList.map((orderItem) => {
                                                if (orderItem.value.orderNo == item.orderNo) {
                                                    orderItem.value.logisticsNo = logisticsNo
                                                    if (this.state.isFranchisee) {
                                                        orderItem.value.sellerConfirm = 1
                                                    } else {
                                                        orderItem.value.buyerConfirm = 1
                                                    }
                                                }
                                            })
                                            this.setState({
                                                orderList: orderList
                                            })
                                        }
                                    })
                                }}
                            >
                                <Text style={styles.btnText}>{'物流确认'}</Text>
                            </TouchableOpacity>
                        </View>
                        :
                        // 双方已确认
                        item.shippingConfirm == 1 && item.deliveryConfirm == 1 ?
                            <View style={styles.btnBg}>
                                <TouchableOpacity
                                    style={styles.btnBox}
                                    onPress={() => {
                                        this.consultOnline(item)
                                    }}
                                >
                                    <Text style={styles.btnText}>{this.state.isFranchisee ? '联系买家' : '联系卖家'}</Text>
                                </TouchableOpacity>
                                <TouchableOpacity
                                    style={styles.btnBox1}
                                    onPress={() => {
                                        navigation.navigate('LogisticsTrackPage', { logisticsNo: item.logisticsNo })
                                    }}
                                >
                                    <Text style={styles.btnText}>{'查看物流'}</Text>
                                </TouchableOpacity>
                            </View>
                            :
                            //己方已确认
                            (item.deliveryConfirm == 1 && !this.state.isFranchisee) || (item.shippingConfirm == 1 && this.state.isFranchisee) ?
                                <View style={styles.btnBg}>
                                    <TouchableOpacity
                                        style={styles.btnBox}
                                        onPress={() => {
                                            this.consultOnline(item)
                                        }}
                                    >
                                        <Text style={styles.btnText}>{this.state.isFranchisee ? '联系买家' : '联系卖家'}</Text>
                                    </TouchableOpacity>
                                    <TouchableOpacity
                                        style={styles.btnBox1}
                                        onPress={() => {
                                            navigation.navigate('NormalOrderDetail', { orderNo: item.orderNo })
                                        }}
                                    >
                                        <Text style={styles.btnText}>{'查看订单'}</Text>
                                    </TouchableOpacity>
                                </View>
                                :
                                null
                }
            </View>
        )
    }

    //卖家待预付状态：2
    renderFranchiseeWaitPayBottom(item) {
        return (
            <View style={styles.btnBg}>
                <View style={styles.btnBg}>
                    <TouchableOpacity
                        style={styles.btnBox}
                        onPress={() => {
                            this.consultOnline(item)
                        }}
                    >
                        <Text style={styles.btnText}>{'联系买家'}</Text>
                    </TouchableOpacity>

                </View>
            </View>
        )
    }

    //买家待预付状态：2
    renderMerchantWaitPayBottom(item) {
        return (
            <View style={styles.btnBg}>
                <View style={styles.btnBg}>
                    <TouchableOpacity
                        style={styles.btnBox}
                        onPress={() => {
                            this.consultOnline(item)
                        }}
                    >
                        <Text style={styles.btnText}>{'联系卖家'}</Text>
                    </TouchableOpacity>
                    <TouchableOpacity
                        style={styles.btnBox1}
                        onPress={() => {
                            this.confirmPay(item, true)
                        }}
                    >
                        <Text style={styles.btnText}>{'确认支付'}</Text>
                    </TouchableOpacity>
                </View>
            </View>
        )
    }

    //卖家待收货状态：3
    renderFranchiseeWaitReceiveBottom(item) {
        return (
            <View style={styles.btnBg}>
                <TouchableOpacity
                    style={styles.btnBox}
                    onPress={() => {
                        this.consultOnline(item)
                    }}
                >
                    <Text style={styles.btnText}>{'联系买家'}</Text>
                </TouchableOpacity>
                {
                    item.isSelfPickup == '0' ? (
                        <TouchableOpacity
                            style={styles.btnBox1}
                            onPress={() => {
                                navigation.navigate('LogisticsTrackPage', { logisticsNo: item.logisticsNo })
                            }}
                        >
                            <Text style={styles.btnText}>{'查看物流'}</Text>
                        </TouchableOpacity>
                    ) : null
                }

            </View>
        )
    }

    //买家待收货状态：3
    renderMerchantWaitReceiveBottom(item) {
        return (
            <View style={styles.btnBg}>
                {
                    //已交车
                    item.logisticsStatus == LogisticsStatus.LogisticsStatusDeliverCar || item.isSelfPickup == '1' ?
                        <View style={styles.btnBg}>
                            <TouchableOpacity
                                style={styles.btnBox}
                                onPress={() => {
                                    this.consultOnline(item)
                                }}
                            >
                                <Text style={styles.btnText}>{'联系卖家'}</Text>
                            </TouchableOpacity>

                            {!item.repetitionDetectionStatus || item.repetitionDetectionStatus == '' ?

                                <TouchableOpacity
                                    style={styles.btnBox1}
                                    onPress={() => {


                                        navigation.navigate('CarDetectionPage', {
                                            carId: item.carId, type: '1', completion: () => {
                                                let orderList = this.state.orderList
                                                orderList.map((orderItem) => {
                                                    if (orderItem.value.orderNo == item.orderNo) {
                                                        orderItem.value.detectionStatus = detectionStatus.detectionStatusWaitForDetection
                                                    }
                                                })
                                                this.setState({
                                                    orderList: orderList
                                                })
                                            }
                                        })
                                    }}
                                >
                                    <Text style={styles.btnText}>{'复检下单'}</Text>
                                </TouchableOpacity>

                                : null}

                        </View>
                        :
                        <View style={styles.btnBg}>
                            <TouchableOpacity
                                style={styles.btnBox}
                                onPress={() => {
                                    this.consultOnline(item)
                                }}
                            >
                                <Text style={styles.btnText}>{'联系卖家'}</Text>
                            </TouchableOpacity>
                            <TouchableOpacity
                                style={styles.btnBox1}
                                onPress={() => {
                                    navigation.navigate('LogisticsTrackPage', { logisticsNo: item.logisticsNo })
                                }}
                            >
                                <Text style={styles.btnText}>{'查看物流'}</Text>
                            </TouchableOpacity>
                        </View>
                }

            </View>
        )
    }

    //卖家待结算状态：4
    renderFranchiseeWaitSettlementBottom(item) {
        return (
            <View style={styles.btnBg}>
                <View style={styles.btnBg}>
                    <TouchableOpacity
                        style={styles.btnBox}
                        onPress={() => {
                            this.consultOnline(item)
                        }}
                    >
                        <Text style={styles.btnText}>{'联系买家'}</Text>
                    </TouchableOpacity>
                    <TouchableOpacity
                        style={styles.btnBox1}
                        onPress={() => {
                            navigation.navigate('NormalOrderDetail', { orderNo: item.orderNo })
                        }}
                    >
                        <Text style={styles.btnText}>{'查看订单'}</Text>
                    </TouchableOpacity>
                </View>
            </View>
        )
    }

    //买家待结算状态：4
    renderMerchantWaitSettlementBottom(item) {
        return (
            <View style={styles.btnBg}>
                <View style={styles.btnBg}>
                    <TouchableOpacity
                        style={styles.btnBox}
                        onPress={() => {
                            this.consultOnline(item)
                        }}
                    >
                        <Text style={styles.btnText}>{'联系卖家'}</Text>
                    </TouchableOpacity>
                    {/* <TouchableOpacity
                        style={styles.btnBox1}
                        onPress={() => {
                            navigation.navigate('ApplySaleServicePage', { orderNo: item.orderNo })
                        }}
                    >
                        <Text style={styles.btnText}>{'申请售后'}</Text>
                    </TouchableOpacity> */}
                    <TouchableOpacity
                        style={styles.btnBox1}
                        onPress={() => {
                            navigation.navigate('ConfirmReceiveCar', { from: '1', orderNo: item.orderNo })
                        }}
                    >
                        <Text style={styles.btnText}>{'确认收货'}</Text>
                    </TouchableOpacity>
                </View>
            </View>
        )
    }

    //买家/卖家售后状态：5
    renderAfterSaleBottom(item) {
        return (
            <View style={styles.btnBg}>
                <TouchableOpacity
                    style={styles.btnBox}
                    onPress={() => {
                        this.consultOnline(item)
                    }}
                >
                    <Text style={styles.btnText}>{this.state.isFranchisee ? '联系买家' : '联系卖家'}</Text>
                </TouchableOpacity>
                <TouchableOpacity
                    style={styles.btnBox1}
                    onPress={() => {
                        this.getAfterSalesLastByOrderNo(item.orderNo)
                    }}
                >
                    <Text style={styles.btnText}>{'处理售后'}</Text>
                </TouchableOpacity>
            </View>
        )
    }

    getAfterSalesLastByOrderNo(orderNo) {
        HttpUtils.getRequest(HttpUrls.getAfterSalesLastByOrderNo, { quotationOrderNo: orderNo })
            .then((result) => {
                if (result.code == 0) {

                    navigation.navigate('AfterSaleOrderDetail', { orderNo: result.data.afterSalesNo })
                } else {

                }
            })
            .catch((error) => {

            });
    }

    consultOnline(item) {
        let param = {
            carId: item.carId,
            buyerMerchantId: item.buyerMerchantId,
            sellerMerchantId: item.sellerMerchantId,
            buyerUserId: item.buyerUserId,

        }
        if (this.Loading) {
            this.Loading.show();
        }

        HttpUtils.postRequest(HttpUrls.quotationConsult, param)
            .then((result) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
                if (result.code == 0) {

                    setTimeout(() => {
                        navigation.navigate('MessageChat', {
                            orderNo: item.orderNo,
                            imGroupId: result.data.imGroupId,
                        })
                    }, 500);

                } else {
                    ToastToUser(result.msg + '')
                }
                l
            })
            .catch((error) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
            });
    }

    fromatBrand(item) {
        let carNameArr = item.value.carName.split('#')
        let carName = item.value.carName
        if (carNameArr.length > 2) {
            carName = carNameArr[1] + ' ' + carNameArr[2]
        }
        return carName
    }

    formatInfo(item) {
        let registerDate = moment(item.value.registerDate).format('YYYY-MM')

        let info = ''
        if (registerDate) {
            info = registerDate
        }
        if (item.value.mileage) {
            if (info) {
                info = info + ' | '
            }
            info = info + (item.value.mileage / 10000).toFixed(2) + '万公里'
        }
        if (item.value.sellerUserCityName) {
            if (info) {
                info = info + ' | '
            }
            info = info + item.value.sellerUserCityName
        }

        return info
    }

    status() {

    }

    renderEmpty() {
        if (!this.state.isRefresh) {
            return (
                <NoData Desc="暂无数据" style={{ marginTop: gREM(200) }} />
            )
        } else {
            return (
                null
            )
        }
    }

    _keyExtractor = (item, index) => index + '';

    renderFooter() {
        if (this.state.showFoot === 1) {
            return <View style={{ height: Constant.bottomHeight }} />;
        } else if (this.state.showFoot === 2) {
            return (
                <View style={styles.footer}>
                    <ActivityIndicator size="small" color="#999" />
                    <Text style={styles.loadMoreTextStyle}>正在加载更多数据...</Text>
                </View>
            );
        } else if (this.state.showFoot === 0) {
            return null;
        }
    }

    reachedEnd() {
        if (this.state.orderList.length == 0) {
            return;
        }
        //如果是正在加载中或没有更多数据了，则返回
        if (this.state.showFoot != 0) {
            return null;
        }

        //底部显示正在加载更多数据
        this.setState({ showFoot: 2 });

        this.getOrders(false);
    }

    refreshData = () => {
        this.getOrders(true);
    }

    getOrders(refresh) {
        if (refresh) {
            pageNo = 1;
            itemNo = 0;
            this.setState({ isRefresh: true, orderList: [] })
        } else {
            pageNo++;
        }

        let param = {
            pageNo: pageNo,
            pageSize: pageSize,
            role: this.state.isFranchisee ? 2 : 1,
        }

        if (this.state.orderStatus === OrderStatus.OrderStatusWaitSign) {
            param['orderStatusList'] = [OrderStatus.OrderStatusWaitSign, OrderStatus.OrderStatusSigning, OrderStatus.OrderStatusRejectSign]
        } else {
            param['orderStatus'] = this.state.orderStatus
        }

        if (this.state.keyword) {
            param['queryStr'] = this.state.keyword
        }

        HttpUtils.postRequest(HttpUrls.getQuotationOrderList, param)
            .then((result) => {
                let data = result.data
                if (data) {
                    let resultList = data.resultList
                    console.log('orderList',resultList)
                    let dataBlob = [];
                    let i = itemNo;
                    if (resultList && resultList != null && resultList.length > 0) {
                        resultList.map(function (item) {
                            dataBlob.push({
                                key: i,
                                value: item,
                            })
                            i++;
                        });
                        itemNo = i;
                        this.setState({
                            orderList: this.state.orderList.concat(dataBlob),
                            isRefresh: false,
                            showFoot: 1,
                        });
                        setTimeout(() => {
                            this.setState({
                                showFoot: dataBlob.length >= pageSize ? 0 : 1,
                            })
                            resultList = null;
                            dataBlob = null;
                        }, 200);
                    } else {
                        this.setState({
                            showFoot: 1,
                            isRefresh: false,
                        });
                    }
                } else {
                    this.setState({
                        showFoot: 1,
                        isRefresh: false,
                    });
                }
            })
            .catch((error) => {
                this.setState({
                    showFoot: 1,
                    isRefresh: false,
                });
            });
    }

    applyDetection(item) {
        let param = {
            orderNo: item.orderNo,
        }
        if (this.Loading) {
            this.Loading.show('申请检测中...')
        }
        HttpUtils.getRequest(HttpUrls.orderApplyDetection, param)
            .then((result) => {
                if (this.Loading) {
                    this.Loading.hide()
                }
                if (result.code == 0) {
                    navigation.navigate('MessageChat', {
                        imGroupId: result.data.imConversationId,
                        carId: item.carId
                    })

                    let orderList = this.state.orderList
                    orderList.map((orderItem) => {
                        if (orderItem.value.orderNo == item.orderNo) {
                            orderItem.value.applyDetection = 1
                        }
                    })
                    this.setState({
                        orderList: orderList
                    })

                } else {
                    ToastToUser(result.msg ? result.msg : result.statusText)
                }
            })
            .catch((error) => {
                if (this.Loading) {
                    this.Loading.hide()
                }
            });
    }

    //确认预付款、尾款
    confirmPay(item, isprePay) {
        let param = {
            orderNo: item.orderNo
        }
        this.Loading.show('加载中...')
        HttpUtils.getRequest(HttpUrls.getDetailsByOrderNo, param)
            .then((result) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
                if (result.code == 0 && result.data) {
                    let data = result.data;
                    this.confirmPayOrder(data.tradePrice, item.orderNo)
                } else {
                    ToastToUser(result.msg ? result.msg : result.statusText);
                }
            })
            .catch((error) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
                ToastToUser(error);
            });
    }

    /**
    * 买家支付车款
    * @param {*} totalAmount 
    */
    confirmPayOrder(totalAmount, orderNo) {
        let param = {
            userId: Constant.userInfo.userId,
            merchantId: Constant.userInfo.merchantId,
            totalAmount: totalAmount,
            payChannel: 10,
            tradeType: 22,
            operator: Constant.userInfo.fullname,
            outTradeNo: orderNo
        }

        if (this.Loading) {
            this.Loading.show()
        }
        HttpUtils.postRequest(HttpUrls.paymentOrderGenerate, param)
            .then((result) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
                let code = result.code
                if (code == 0) {
                    let data = result.data
                    let channelResult = data.channelResult
                    if (channelResult) {

                        this.props.navigation.navigate('WebView', { source: channelResult })
                    }
                } else {
                    ToastToUser(result.msg + '')
                }
            })
            .catch((error) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
            });
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#f9f9f9'
    },
    orderBg: {
        width: '100%',
        flexDirection: 'column',
    },
    sepLine: {
        width: '100%',
        height: 10,
        backgroundColor: '#F9F9F9',
    },
    orderContainer: {
        paddingHorizontal: 16,
        paddingBottom: 12,
        backgroundColor: '#fff'
    },
    topContainer: {
        width: '100%',
        height: 44,
        flexDirection: 'row',
        justifyContent: 'space-between',
        alignItems: 'center',
        borderBottomColor: '#E4E4E4',
        borderBottomWidth: 0.3
    },
    companyText: {
        fontSize: 14,
        color: '#333333'
    },
    statusText: {
        fontSize: 14,
        color: '#FF7B00'
    },
    carContainer: {
        flexDirection: 'row',
        marginVertical: 12,
        flexDirection: 'row',
    },
    carImg: {
        backgroundColor: '#f9f9f9',
        width: 130,
        height: 90,
        borderRadius: 4
    },
    carInfoBox: {
        marginLeft: 12,
        flex: 1,
        flexDirection: 'column',
        alignItems: 'flex-start',
    },
    brandText: {
        marginTop: 5,
        fontSize: 14,
        lineHeight: 18,
        fontWeight: '500',
        color: '#333333'
    },
    carInfoText: {
        marginTop: 5,
        fontSize: 12,
        color: '#666666'
    },
    priceText: {
        fontSize: 14,
        color: '#ED542E',
        position: 'absolute',
        bottom: 5,
    },
    btnBg: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'flex-end'
    },
    btnBox: {
        flexDirection: 'row',
        alignItems: 'center',
        paddingHorizontal: 14,
        height: 32,
        borderRadius: 16,
        borderWidth: 1,
        borderColor: '#E5E5E5'
    },
    btnBox1: {
        marginLeft: 10,
        flexDirection: 'row',
        alignItems: 'center',
        paddingHorizontal: 14,
        height: 32,
        borderRadius: 16,
        backgroundColor: '#FFE000'
    },
    btnText: {
        fontSize: 14,
        color: '#333333'
    },

    footer: {
        flexDirection: 'row',
        height: 40,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#f6f6f6'
    },
    loadMoreTextStyle: {
        fontSize: 14,
        color: '#999999',
        marginStart: 10,
    },
})